package com.baosight.risk.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * @author duanyongqiang
 * <p>
 * 读取properties 文件
 */
@Slf4j
public class ReadProperties {
    private static final String file_name = "application.properties";

    public static List<String> propertiesToList(String path) {
        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        String tempString = null;
        try {
            path = ReadProperties.class.getClassLoader().getResource(path + ".properties").getPath();
            File file = new File(path);
            if (file.exists()) {
                reader = new BufferedReader(new FileReader(file));
                while ((tempString = reader.readLine()) != null) {
                    list.add(tempString);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        }
        return list;
    }

    /**
     * load properties
     *
     * @param propertyFileName
     * @return
     */
    public static Properties loadProperties(String propertyFileName) {
        Properties prop = new Properties();
        InputStreamReader in = null;
        try {
            URL url = null;
            ClassLoader loder = Thread.currentThread().getContextClassLoader();
            url = loder.getResource(propertyFileName);
            in = new InputStreamReader(new FileInputStream(url.getPath()), "UTF-8");
            prop.load(in);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return prop;
    }

    public static String getValueString(String key) {
        Properties prop = loadProperties(file_name);
        if (prop != null) {
            if (prop.getProperty(key)!=null){
                return prop.getProperty(key);
            }else{
                //查询多properties
                String property = prop.getProperty("spring.profiles.active");
                if (property!=null){
                    prop = loadProperties("application-"+property+".properties");
                    if (prop!=null){
                        return prop.getProperty(key);
                    }
                }
            }
        }
        return null;
    }

    public static void main(String[] ages) {
//		List<String> whiteUser = propertiesToList("whiteUser");
//		System.out.println(whiteUser.toString());
        String projectFilePath = getValueString("projectServerFilePath");
        System.out.println(projectFilePath);
    }
}
